﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI
{

    public static class XEventManager
    {
        private static List<XEventModel> s_modelList = new List<XEventModel>();

        public static XEvent Register(Type partType, string name, params XEventArgument[] args)
        {
            XEvent @event = new XEvent(name, args.ToArray());
            bool isExist = false;
            foreach (var item in s_modelList)
            {
                if (item.PartType == partType)
                {
                    isExist = true;
                    item.AddEvent(@event);
                }
                else if (item.PartType.IsSubclassOf(partType))
                {
                    item.AddEvent(@event);
                }
            }

            if (!isExist)
            {
                XEventModel eventModel = new XEventModel(partType);
                eventModel.AddEvent(@event);
                s_modelList.Add(eventModel);
            }
            return @event;
        }

        public static XEvent[] GetEvents(Type partType)
        {
            List<XEvent> events = new List<XEvent>();
            foreach (var item in s_modelList)
            {
                if (item.PartType == partType)
                {
                    events.AddRange(item.EventList);
                }
                else if (partType.IsSubclassOf(item.PartType))
                {
                    // 检查父类有没有事件
                    events.AddRange(item.EventList);
                }
            }
            return events.ToArray();
        }
    }

}
